﻿using ChuanYe.CountyFireManagement.EF.MySQL.Models;
using ChuanYe.CountyFireManagement.Model;
using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ChuanYe.CountyFireManagement.BLL
{
    public class GpsDeviceBLL
    {
        #region 构造单例
        private GpsDeviceBLL() { }
        private static GpsDeviceBLL _instance;
        public static GpsDeviceBLL Instance
        {
            get
            {
                return _instance ?? (_instance = new GpsDeviceBLL());
            }
        }
        #endregion


        public int Add(Gpsdevice info) 
        {
            using (CountyFireManagementContext ef = new CountyFireManagementContext())
            {
                var exist = ef.Gpsdevice.Where(p => p.GpsDeviceNo == info.GpsDeviceNo).FirstOrDefault();
                if (exist != null)
                {
                    return -2;
                }
                ef.Gpsdevice.Add(info);
                ef.Entry<Gpsdevice>(info).State = EntityState.Added;
                return ef.SaveChanges();
            }
        }

        public int Update(Gpsdevice info)
        {
            using (CountyFireManagementContext ef = new CountyFireManagementContext())
            {
                var exist = ef.Gpsdevice.Where(p => p.GpsDeviceId == info.GpsDeviceId).FirstOrDefault();
                if (exist == null)
                {
                    return -1;
                }
                else
                {
                    int isUpdate = ef.Gpsdevice.Where(p => p.GpsDeviceNo == info.GpsDeviceNo && p.GpsDeviceId != info.GpsDeviceId).ToList().Count();
                    if (isUpdate != 0)
                    {
                        return -2;
                    }
                }
                exist.GpsDeviceName = info.GpsDeviceName;
                exist.GpsDeviceNo = info.GpsDeviceNo;
                ef.Gpsdevice.Add(exist);
                ef.Entry<Gpsdevice>(exist).State = EntityState.Modified;
                return ef.SaveChanges();
            }
        }

        public int Remove(int gpsDeviceId)
        {
            using (CountyFireManagementContext ef = new CountyFireManagementContext())
            {
                var exist = ef.Gpsdevice.Where(p => p.GpsDeviceId == gpsDeviceId).FirstOrDefault();
                if (exist == null)
                {
                    return -1;
                }
                var existGpsDeviceConfigList = ef.Gpsdeviceconfig.Where(p=>p.GpsDeviceId==gpsDeviceId).ToList();
                foreach (var item in existGpsDeviceConfigList)
                {
                    ef.Gpsdeviceconfig.Add(item);
                    ef.Entry<Gpsdeviceconfig>(item).State = EntityState.Deleted;
                    return ef.SaveChanges();
                }
                ef.Gpsdevice.Add(exist);
                ef.Entry<Gpsdevice>(exist).State = EntityState.Deleted;
                return ef.SaveChanges();
            }
        }


        public Gpsdevice Get(string gpsDeviceNo)
        {
            using (CountyFireManagementContext ef = new CountyFireManagementContext())
            {
                return ef.Gpsdevice.Where(p => p.GpsDeviceNo == gpsDeviceNo).FirstOrDefault();
            }
        }
    }
}
